<?php
class Student extends CI_Controller {
     
	  function __construct()
	{
		parent::__construct();	
	    $this->load->model('student_model');
		$this->load->model('form_model');
		$this->load->model('payment_model');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		if(isset($_POST['search']))
		{
			$course              = $this->input->post('course');         // filter subject with course 
			$view['filter1']     = $course ;  
			$this->session->set_userdata(array('filter' => $course));    // set filter value
			
		}
		else
		{
			$view['filter1']     = $this->session->userdata('filter');
		}
		if(!empty($view['filter1']))
		{
		$like['name']             = $view['filter1'];   //$course;
		}
		$where                    = array('deleted' => 0);
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		$view['students'] 		  = $students->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Users Listing';
		$view['subtitle']	      = 'Users Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/student/index/';
		$config['total_rows'] 	  = $students->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('users_msg'));
		$this->layout->adminview('students_view', $view);
	
	}
	function myaccount()
	{
		$view['title']		      = 'Edit Account';
		$view['subtitle']	      = 'Edit Account';
		$view['menu_var']         = 5;
		$mylogin                 = $this->session->userdata('mylogin');
		$where                    = array('users.id'=>$mylogin['id'],'users.deleted' => 0);
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		$view['student'] 		  = $students->records[0];
		$save                     = $this->input->post('save');
		$resume                   = $this->input->post('resume');
		$photo                    = $this->input->post('photo');
		if(!empty($save)) 
		{
			$data                      = $this->retrieve->post($this->student_model->fields);
			if(!empty($_FILES['resume']['name']))
			{
			$data['resume_type']       = end(explode('.', $_FILES['resume']['name']));
			}
			else
			$data['resume_type']       = $view['student']['resume_type'];
			if(!empty($_FILES['photo']['name']))
			{
			$data['photo_type']        = end(explode('.', $_FILES['photo']['name']));
			}
			else
			$data['photo_type']       = $view['student']['photo_type'];
			$insertStatus              = $this->student_model->udpate($mylogin['id'], $data);
			
			/************************ upload photo *******************/
			       
			$ext                   = $data['photo_type'];
			$ext1                  = $data['resume_type'];
			//echo $_FILES['photo']['type'];
		    if(!empty($_FILES['photo']))
			{
				if (!(($_FILES['photo']['type'] =="image/gif") || ($_FILES['photo']['type'] =="image/jpeg") || ($_FILES['photo']['type'] =="image/pjpeg")))
				{
					$view['err1']= "Your uploaded file must be jpg, gif<BR>";
				}
				else
				{
				
					$l_image_path           = "fpanel/images/fotos/".$mylogin['id'].".".$ext; // the path with the file name where the file will be stored,  
			
					if(move_uploaded_file ($_FILES['photo']['tmp_name'],$l_image_path))
					{
					chmod("$l_image_path",0777);
					}
					else
					{
						$view['err2']= "Failed to upload file Contact Site admin to fix the problem";
	exit;
					}
				}
			}
			//echo $_FILES['resume']['type'];
				if(!empty($_FILES['resume']))
			{
				if (!(($ext1 =="doc") || ($ext1 =="docx")))
				{
					$view['err3'] ="Your uploaded file must be of type doc or docx. Other file types are not allowed<BR>";
				}
				else
				{
				
					$l_image_path           = "fpanel/images/resumes/".$mylogin['id'].".".$ext1; // the path with the file name where the file will be stored,  
			
					if(move_uploaded_file ($_FILES['resume']['tmp_name'],$l_image_path))
					{
					chmod("$l_image_path",0777);
					}
					else
					{
						$view['err4']= "Failed to upload file Contact Site admin to fix the problem";
	//exit;
					}
				}
			} 
			/************************ upload photo ******************/
			//echo $insertStatus;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'form_edit_success');
					//$view['msg']=$this->lang->line('form_edit_success');
					redirect($this->config->item('member_base').'student/account/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
			}
		}
		$this->layout->memberview('myaccount_view', $view);
	}
	function account()
	{
		$view['title']	          = 'My Profile';
		$view['subtitle']	      = 'Profile';
		$view['menu_var']         = 1;
		$mylogin = $this->session->userdata('mylogin');
		$where                    = array('users.id'=>$mylogin['id'],'users.deleted' => 0);
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		$view['student'] 		  = $students->records[0];
		$form                     = $this->form_model->getForms(0,1,'',array('id'=>$view['student']['country_id']));
		$view['form']             = $form->records[0];
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('users_msg'));
		$this->layout->memberview('home', $view);
	}
	function payment()
	{
		$view['title']	          = 'My Payments';
		$view['subtitle']	      = 'Payments';
		$view['menu_var']         = 4;
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('users_msg'));
		$mylogin = $this->session->userdata('mylogin');
		$where                    = array('users.id'=>$mylogin['id'],'users.deleted' => 0);
		$students                 = $this->student_model->getStudents($page, $per_page_count , $like, $where );
		$view['student'] 		  = $students->records[0];
		$form                     = $this->form_model->getForms(0,1,'',array('id'=>$view['student']['country_id']));
		$view['form']             = $form->records[0];
	    $payments                 = $this->payment_model->getPayments(0,'','',array('user_id'=>$mylogin['id']));
		$view['payments']         = $payments->records;
		$save                     = $this->input->post('save');
		if(!empty($save))
		{
			$data                 = $this->retrieve->post($this->payment_model->fields);
			$insertStatus         = $this->payment_model->add($data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'payment_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('member_base').'student/payment/'.$cur_page,'location');
					else
						redirect($this->config->item('member_base').'student/payment/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['err_msg'] 	= $this->lang->line('payment_valid_failed');
					break;
				case -3:
					$view['err_msg'] 	= $this->lang->line('payment_already_exists');
					break;
				case false:
					$view['err_msg'] 	= $this->lang->line('payment_add_failure');
	
					break;
				
				default:
					$view['err_msg'] 	= $this->lang->line('form_add_failure');
					break;
			}
		}
		$this->layout->memberview('payment', $view);
		
		
	}
	function bank()
	{
		$view['title']            = "Account details";
	    $view['subtitle']         = "Account details";
		$view['menu_var']         = 4;
		$this->layout->memberview('bank', $view);
	}

	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'registration/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->student_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'student_delete_success');
					redirect($this->config->item('admin_base').'student/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('course_delete_failure');
					break;
			}
		}
		
	}
	
	
	
}
?>